﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonsLib.Utility;
using System.Data.SqlClient;
using MaxLeaf.Platform.Functions;
using System.Data.Common;
using MaxLeaf.Platform.Commons;
using MaxLeaf.Platform.DataContract;
using CommonsLib.DB;

namespace MaxLeaf.Platform.DALImplementation
{
    public class goods_UserKeywordsDAL : BaseDAL
    {
         

        public void GetUserKeywords(DBAccess dbcon, int intCompanyID, ref string strKeywordsTemplate, ref int intIndex,ref int intLen)
        {
            while (true)
            {
                string strSql = @"select usegoods_name from goods_UserKeywords  
                where company_id=@CompanyID and usegoods_name  =@name                
            ";
                using (DbDataReader dr = dbcon.GetDataFirstRow(strSql, new SqlParameter[]{
                new SqlParameter("@CompanyID",intCompanyID.ToString())
                ,new SqlParameter("@name",string.Format("{0}{1}",strKeywordsTemplate,BaseFunc.FormatWithZero(intIndex.ToString(),intLen)))                
            }))
                {
                    if (dr.Read())
                    {
                        intIndex++;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }

        public void GetMaxUserKeywordsIndex(DBAccess dbcon, int intCompanyID, ref string strKeywordsTemplate, ref int intIndex, ref int intLen)
        {
            string strSql =string.Format(@"select cast(right(usegoods_name,{0}) as int) from goods_UserKeywords  
                where company_id=@CompanyID and usegoods_name  >=@stname and usegoods_name<=@edname
                and dbo.F_IsIntNum(right(usegoods_name,{0}))=1 and cast(right(usegoods_name,{0}) as int)>=@index
            ", intLen);
            using (DbDataReader dr = dbcon.GetDataFirstRow(strSql,new SqlParameter[]{
                new SqlParameter("@CompanyID",intCompanyID.ToString())
                ,new SqlParameter("@stname",string.Format("{0}{1}",strKeywordsTemplate,BaseFunc.FormatWithZero("0",intLen)))
                ,new SqlParameter("@edname",string.Format("{0}{1}",strKeywordsTemplate,BaseFunc.FormatWithZero("9",intLen)))
                ,new SqlParameter("@index",intIndex.ToString())
            }))
            {
                if(dr.Read())
                {
                    intIndex = dr.GetInt32(0) + 1;
                }
            }
        }
    }
}
